﻿using Mitek.Models.Mikek;
using Mitek.Services.Interfaces.Mitek;
using Mitek.Web.App_Start;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Mitek.Web.Controllers
{
    public class ReportController : Controller
    {
        private readonly IMitekDataService _imitekdataservice;

        public ReportController()
        {

        }
        public ReportController(IMitekDataService imitekdataservice)
        {
            this._imitekdataservice = imitekdataservice;
        }
        // GET: Report
        public ActionResult Index()
        {
            return View();
        }

        public List<Menu> GetReportField()
        {
            var path = System.Web.HttpContext.Current.Server.MapPath("json.json");
            FileStream fs1 = new FileStream(path, FileMode.Open);
            StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("GB2312"));
            string str1 = sr.ReadToEnd();
            sr.Close();
            return JsonDeserialize<List<Menu>>(str1);

        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (SessionContext.CurrentUser.ID > 0)
                {
                    MitekData mitekdata = new MitekData
                    {
                        M1 = collection["M1"],
                        M2 = collection["M2"],
                        M3 = collection["M3"],
                        M4 = collection["M4"],
                        M5 = collection["M5"],
                        M6 = collection["M6"],
                        M7 = collection["M7"],
                        M8 = collection["M8"],
                        M9 = collection["M9"],
                        M10 = collection["M10"],
                        M11 = collection["M11"],
                        M12 = collection["M12"],
                        M13 = collection["M13"],
                        M14 = collection["M14"],
                        ArtherxCount = collection["ArtherxCount"],
                        SmithCount = collection["SmithCount"],
                        CreatedTime = DateTime.Now,
                        ModifiedTime = DateTime.Now,
                        IsDeleted = false,
                        CreateUserID = SessionContext.CurrentUser.ID

                    };
                    _imitekdataservice.AddMitekData(mitekdata);
                    return Json(new { Result = "success", Url = "/report/create", msg = "Mitek数据上报成功，页面正在跳转，请稍等..." });
                }
                else
                {
                    return Json(new { Result = "error", Url = "/user/login", msg = "登录信息过期，请重新登录..." });
                }

            }
            catch (Exception ex)
            {
                return Json(new { Result = "error", msg = ex.Message });
            }
        }

        public ActionResult Search()
        {
            return View();
        }

        #region .net自带
        /// <summary>
        /// .net自带 JSON反序列化
        /// </summary>
        public  T JsonDeserialize<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T obj = (T)ser.ReadObject(ms);
            return obj;
        }
        #endregion
    }

    public class Menu 
    {
        public int Dept { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public List<MenuChildF> MenuChildF { get; set; }
    }

    public class MenuChildF
    {
        public int Dept { get; set; }
        public string Name { get; set; }
        public int Fid { get; set; }
        public List<MenuChildS> MenuChildS { get; set; }
    }

    public class MenuChildS
    {
        public int Dept { get; set; }
        public string Name { get; set; }
        public int Fid { get; set; }
        public string FName { get; set; }
    }
}